<script setup lang="ts">
// 获取屏幕边界到安全区域距离
import { onLoad } from '@dcloudio/uni-app'
import { getUserInfo } from '@/services/user/user'
import { ref } from 'vue'
import type { LoginResult } from '@/types/member'
import { useUserStore } from '@/stores'

const user = ref<LoginResult>()
const avatar = ref<string>('')

/**
 * 获取用户信息
 */
const getMemberProfileData = async () => {
  const userStore = useUserStore()
  const id = userStore.userinfo?.id
  const { data } = await getUserInfo(String(id))
  user.value = data as LoginResult
  if (user.value?.avatar != null) {
    avatar.value = user.value?.avatar
  }
}

/**
 * 更新头像
 */
const onAvatarChange = () => {
  // 调用拍照/选择图片
  uni.chooseImage({
    count: 1,
    sizeType: ['image'],
    success: (res) => {
      const tempFilePath = res.tempFilePaths[0]
      // 文件上传
      uni.uploadFile({
        url: '/system/users/uploadAvatar',
        name: 'file', // 后端数据字段名
        filePath: tempFilePath, // 新头像
        formData: {
          id: user.value?.id,
        },
        success: (res) => {
          if (res.statusCode === 200) {
            // 提取头像
            const { data } = JSON.parse(res.data)
            // 当前页面更新头像
            avatar.value = data.avatar
            // 更新 Store 头像
            useUserStore().userinfo!.avatar = data.avatar
            uni.showToast({ icon: 'success', title: '更新成功' })
          } else {
            uni.showToast({ icon: 'error', title: '出现错误' })
          }
        },
      })
    },
  })
}

onLoad(() => {
  getMemberProfileData()
})
</script>

<template>
  <view class="viewport">
    <!-- 头像 -->
    <view class="avatar">
      <view class="avatar-content">
        <image class="image" :src="avatar" mode="aspectFill" @tap="onAvatarChange" />
        <text class="text" @tap="onAvatarChange">点击修改头像</text>
      </view>
    </view>
    <!-- 表单 -->
    <view class="form">
      <!-- 表单内容 -->
      <view class="form-content">
        <view class="form-item">
          <text class="label">账号</text>
          <text class="account">{{ user?.username }}</text>
        </view>
        <view class="form-item">
          <text class="label">昵称</text>
          <text class="account">{{ user?.nickname }}</text>
        </view>
        <view class="form-item">
          <text class="label">性别</text>
          <text class="account">{{ user?.sex === 0 ? '男' : '女' }}</text>
        </view>
        <view class="form-item">
          <text class="label">手机号</text>
          <text class="account">{{ user?.phone }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<style lang="scss">
page {
  background-color: #f4f4f4;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding-top: 60rpx;
  background: linear-gradient(to bottom, #f1dade 0%, #ffffff 20%);
}

// 导航栏
.navbar {
  position: relative;

  .title {
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 16px;
    font-weight: 500;
    color: #fff;
  }

  .back {
    position: absolute;
    height: 40px;
    width: 40px;
    left: 0;
    font-size: 20px;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

// 头像
.avatar {
  text-align: center;
  width: 100%;
  height: 260rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  .image {
    width: 160rpx;
    height: 160rpx;
    border-radius: 50%;
    background-color: #eee;
  }

  .text {
    display: block;
    padding-top: 20rpx;
    line-height: 1;
    font-size: 26rpx;
    color: #999;
  }
}

// 表单
.form {
  background-color: transparent;
  box-shadow: 0 0 10rpx #e3e3e3;
  margin: 20rpx 30rpx;
  border-radius: 20rpx;

  &-content {
    margin: 20rpx 20rpx 0;
    padding: 0 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }

  &-item {
    display: flex;
    height: 96rpx;
    line-height: 46rpx;
    padding: 25rpx 10rpx;
    background-color: #fff;
    font-size: 28rpx;
    border-bottom: 1rpx solid #ddd;

    &:last-child {
      border: none;
    }

    .label {
      width: 180rpx;
      color: #333;
    }

    .account {
      color: #666;
    }

    .input {
      flex: 1;
      display: block;
      height: 46rpx;
    }

    .radio {
      margin-right: 20rpx;
    }

    .picker {
      flex: 1;
    }

    .placeholder {
      color: #808080;
    }
  }

  &-button {
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
    margin: 30rpx 20rpx;
    color: #fff;
    border-radius: 80rpx;
    font-size: 30rpx;
    background-color: #27ba9b;
  }
}
</style>
